package com.xunk.equipment.service.ruying;

import com.xunk.common.core.exception.CustomException;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.domain.EquipmentPropertiesData;
import com.xunk.equipment.service.BaseService;
import com.xunk.equipment.service.IFreshAirService;
import com.xunk.equipment.vo.response.FreshAirResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunk.common.core.domain.EquipmentResponse;

import java.util.*;
import java.util.function.BiFunction;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.service.ruying
 * @ClassName: FreshAirService
 * @Author: tangguangyuan
 * @Description: 新风service
 * @Date: 2021/4/1 11:08
 * @Version: 1.0
 */

@Service("ruying-fresh-air")
public class FreshAirService implements IFreshAirService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RuYingControlService ruYingControlService;

    @Autowired
    BaseService baseService;

    private static String ON_ID = ".2.1";
    private static String FAN_LEVEL = ".2.2";

    private BiFunction<String,String,String> buildPid = (String deviceSn, String Id)->{ return deviceSn.concat(Id);};

    //设置开关
    public EquipmentPropertiesData setOn(String deviceId,Boolean value){

        //测试
        return createEquipmentPropertiesData(buildPid.apply(deviceId,ON_ID),value);
    }

    //设置风力 2 3 4
    public EquipmentPropertiesData setFanLevel(String deviceId,int value){
        if(value!=2&&value!=3&&value!=4)
            throw new CustomException("风力大小非法");

        //测试
        return createEquipmentPropertiesData(buildPid.apply(deviceId,FAN_LEVEL),value);
    }

    /**
     * 获取设备影子
     * */
    @Override
    public EquipmentResponse getEquipmentInfo(EquipmentDO equipmentDO) {

//        String deviceSn = equipmentDO.getDeviceSn();
//        Long deptId = equipmentDO.getDeptId();
//        Map<String, String> cacheMap = ruYingControlService.getShadowByDeviceSn(deviceSn, deptId);
//
//        FreshAirResponse response = new FreshAirResponse();
//
//        Boolean on = Boolean.parseBoolean(cacheMap.get(buildPid.apply(deviceSn,ON_ID)));
//
//        int fanLevel = Integer.parseInt(cacheMap.get(buildPid.apply(deviceSn,FAN_LEVEL)));
//
//        response.setDeviceSn(deviceSn);
//        response.setFanLevel(fanLevel);
//        response.setOn(on);
//        response.setFanLevelList(fanLevelList);

        /**
         * 测试数据
         */
        FreshAirResponse response = new FreshAirResponse();
        response.setDeviceSn(equipmentDO.getDeviceSn());
        response.setFanLevel(2);
        response.setOn(true);

        FreshAirService freshAirService = new FreshAirService();

        response.setFanLevelList(freshAirService.fanLevelList);
        response.setSupplier(equipmentDO.getSupplier());
        response.setCandidateModel(baseService.getCandidateModel(equipmentDO.getDeviceBaseId()));
        return response;
    }



    //风力预设值
    public List<Map<String,String>> fanLevelList = new ArrayList<Map<String, String>>(4){{
        add(createMap("弱风","2"));
        add(createMap("中风","3"));
        add(createMap("强风","4"));
    }};

    public Map<String,String> createMap(String key,String value){
        Map<String, String> map = new HashMap<String, String>();
        map.put(key,value);
        return map;
    }

    @Override
    //设置设备属性
    public Boolean control(EquipmentDO equipmentDO, String type, String value) {
//        EquipmentPropertiesData equipmentPropertiesData;
//        List<EquipmentPropertiesData.PropertyData> properties = new ArrayList<>();
//        String userId = baseService.findOwnIdByDeptId(equipmentDO.getDeptId());
//        String deviceId = equipmentDO.getDeviceSn();
//
//        switch (type){
//            case "on":
//                equipmentPropertiesData = setOn(deviceId, Boolean.parseBoolean(value));
//                break;
//            case "fanLevel":
//                equipmentPropertiesData = setFanLevel(deviceId, Integer.parseInt(value));
//                break;
//            default:
//                throw new CustomException("控制指令非法");
//        }
//
//        return Objects.equals(ruYingControlService.setProperties(userId,equipmentPropertiesData),"success");

        return true;

    }

    /**
     * 创建EquipmentPropertiesData
     * */
    public EquipmentPropertiesData createEquipmentPropertiesData(String pid ,Object value){
        EquipmentPropertiesData equipmentPropertiesData = new EquipmentPropertiesData();
        List<EquipmentPropertiesData.PropertyData> propertyData = new ArrayList<EquipmentPropertiesData.PropertyData>(1){{
            add(new EquipmentPropertiesData.PropertyData(pid,value));
        }};
        equipmentPropertiesData.setProperties(propertyData);
        //测试
        return equipmentPropertiesData;

    }
}
